<template>
  <!-- 车辆库存 > 车辆出库 -->
  <div>
    <TopFilter
      :list="topFilterList"
      :cols="3"
      @filterChange="changeHandle"
      @onCollapse="collapseHandle"
    ></TopFilter>
    <FilterTable
      ref="table"
      :columns="columns"
      :fetchapi="fetchApi"
      :params="fetchParams"
      :datakey="'records'"
      :isSelectColumn="true"
      :onColumnsChange="columns => this.columns = columns"
      :onRowSelectChange="getSelectData"
    >
      <template slot="controls">
        <el-button size="small" type="primary" icon="el-icon-plus" @click="addHandleBtn">新增</el-button>
      </template>
    </FilterTable>
    <BaseDialog
        :visible.sync="visible"
        :title="dialogTitle"
        destroyOnClose
        width="1050px"
        :containerStyle="{height: '100%', overflow: 'auto'}">
        <DriveModal :type="showType" :rowData="panelData" @close="closeHandler"></DriveModal>
    </BaseDialog>
  </div>
</template>
<script>
import { getSoInvoice, postSoInvoice, queryInvoiceById, querySalesOrderVin } from '@/api/capitalsettle/saleSettlement/ordermanage';
import DriveModal from './driveModal'
import { dictionary } from '@/utils/dictMixin';
import { formatDates,formatDat } from '@/utils/validate';
import { notifyAction } from '@/utils';
import moment from 'moment'
export default {
  name: 'SaleSettlementOrderManage',
  mixins: [dictionary],
  components: {
    DriveModal
  },
  data () {
    return {
      topFilterList: this.createTopFilters(), // 查询区条件
      columns: this.createTableColumns(), // 列表区数据
      fetchApi: getSoInvoice, // 条件查询接口
      fetchParams: {}, // 查询参数
      panelData: {},
      showType: 'SHOW',
      visible: false,
      dialogTitle: '开票登记明细'
    }
  },
  created () {
  },
  mounted () {
    this.BaseTable = this.$refs.table.$refs.pageTable;
  },
  methods: {
    changeHandle (val){
      let { preDateHandler } = this
      val.invoiceDateStart = preDateHandler(val.invoiceDateStart,'YYYY-MM-DD')
      val.invoiceDateEnd = preDateHandler(val.invoiceDateEnd,'YYYY-MM-DD')
      val.updatedAtStart = preDateHandler(val.updatedAtStart,'YYYY-MM-DD')
      val.updatedAtEnd = preDateHandler(val.updatedAtEnd,'YYYY-MM-DD')
      this.fetchParams = Object.assign({}, val);
      console.log(this.fetchParams)
    },
    collapseHandle() {
      this.$nextTick(() => {
        this.BaseTable.EXECUTE_RESET_HEIGHT();
      });
    },
    createTopFilters () {
      return [
        {
          type: 'INPUT',
          label: '销售订单号',
          fieldName: 'viNo',
          placeholder: '请输入...',
        },
        {
          type: 'INPUT',
          label: '开票客户',
          fieldName: 'invoiceCustomer',
          placeholder: '请输入...'
        }, {
          type: 'INPUT',
          label: 'VIN',
          fieldName: 'vin',
          placeholder: '请输入...'
        }, {
          type: 'INPUT',
          label: '发票号码',
          fieldName: 'invoiceNo',
          placeholder: '请输入...'
        }, {
          type: 'RANGE_DATE',
          label: '开票日期',
          fieldName: 'invoiceDateStart|invoiceDateEnd',
          placeholder: '请选择...',
        }, {
          type: 'RANGE_DATE',
          label: '登记日期',
          fieldName: 'updatedAtStart|updatedAtEnd',
          placeholder: '请选择...'
        }
      ]
    },
    createTableColumns () {
      return [
        {
          title: '操作',
          dataIndex: 'column-action',
          width: 100,
          fixed: 'left',
          render: (props, h) => {
            return (
              <div>
                <el-button size="mini" type="text" onClick={() => this.editHandleFun(props.row)}>
                  编辑
                </el-button>
                <el-button size="mini" type="text" onClick={() => this.detailHandleFun(props.row)}>
                  明细
                </el-button>
              </div>
            );
          }
        },
        {
          title: '序号',
          dataIndex: 'index',
          width: 80,
          sorter: true,
          render: props => {
            return <span>{props.row.index + 1}</span>;
          }
        },
        {
          title: '销售单号',
          dataIndex: 'viNo',
          sorter: true,
          filter: true,
          filterType: 'input',
          width: 160,
        },
        {
          title: 'VIN',
          dataIndex: 'vin',
          sorter: true,
          filter: true,
          filterType: 'input',
          width: 160,
        },
        {
          title: '开票客户',
          dataIndex: 'invoiceCustomer',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        /* {
          title: '原发票号码',
          dataIndex: 'invoiceNoOriginal',
          sorter: true,
          filter: true,
          filterType: 'input'
        }, */
        {
          title: '发票号码',
          dataIndex: 'invoiceNo',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '发票类型',
          dataIndex: 'invoiceKind',
          sorter: true,
          filter: true,
          filterType: 'checkbox',
          filterItems: this.createDictList('1207'),
          render: props => {
            return <span>{this.preDictHandler(props.row.invoiceKind, '1207')}</span>
          }
        },
        {
          title: '费用类型',
          dataIndex: 'invoiceChargeType',
          sorter: true,
          filter: true,
          filterType: 'checkbox',
          filterItems: this.createDictList('1421'),
          render: props => {
            return <span>{this.preDictHandler(props.row.invoiceChargeType, '1421')}</span>
          }
        },
        {
          title: '发票金额',
          dataIndex: 'invoiceAmount',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '经办人',
          dataIndex: 'transactor',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '开票日期',
          dataIndex: 'invoiceDate',
          sorter: true,
          filter: true,
          filterType: 'input',
          render: props => {
            return <span> { this.preDateHandler(props.row.invoiceDate, 'YYYY-MM-DD') } </span>
          }
        },
        {
          title: '登记日期',
          dataIndex: 'updatedAt',
          sorter: true,
          filter: true,
          width: 140,
          filterType: 'input',
          render: props => {
            console.log(props.row.updatedAt,"props.row.updatedAt")
            return <span> { formatDates(props.row.updatedAt) } </span>
          }
        },
        {
          title: '登记人',
          dataIndex: 'updatedBy',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
      ]
    },
    addHandleBtn () {
      this.visible = true;
      this.dialogTitle = '开票登记新增'
      this.showType = 'add'
    },
    editHandleFun (row) {
      console.log(row)
      this.visible = true;
      this.dialogTitle = '开票登记编辑'
      this.showType = 'edit'
      this.panelData = row
    },
    detailHandleFun (row) {
      this.visible = true;
      this.dialogTitle = '开票登记明细'
      this.showType = 'show'
      this.panelData = row
    },
    getSelectData () {

    },
    // 字典数据使用前预处理
    preDictHandler(val, code){
      let resultText = val && String.prototype.includes.call(val, code) ? this.createDictText(val, code) : val;
      return resultText;
    },
    // 日期预处理
    preDateHandler(val, formatCode='YYYY-MM-DD HH:mm:ss') {
      let resultDate = val ? moment(Number(val)).format(formatCode) : val;
      if ((resultDate == 'Invalid date' || resultDate == 'Invalid Date') && val) {
        resultDate = moment(val).format(formatCode)
      }
      return resultDate;
    },
    closeHandler (val, type) {
      this.visible = val;
      if (type === 'refresh') {
        // 重新加载
        this.fetchParams = { ...this.fetchParams };
      }
    }
  }
}
</script>
<style lang="less" scoped>
</style>
